package Game;

import java.util.Random;

public class Ant {
	private int age = 0;
	private int size = 1;
	private Species species;
	private int strength = 0;
	private int hp = Settings.HP;
	private Genre genre;
	private int expirence;
	private int x;
	private int y;
	private int minMyX;
	private int minMyY;
	private int maxMyX;
	private int maxMyY;
	public int getAge() {
		return age;
	}

	public void setAge(int age) {

		this.age = age;
	}
	
	public void makeOlder() {
		if(this.age++==Settings.MAX_AGE) {
			hurt(10);
		} 
		if(this.age%10==0) {
			growStrength();
		}
	}
	
	public int getSize() {
		return size;
	}

	public void setSize(int size) {
		this.size = size;
	}

	public Species getSpecies() {
		return species;
	}

	public int getStrength() {
		return strength;
	}

	public void setStrength(int strength) {
		this.strength = strength;
	}

	public void growStrength() {
		if(strength!=10) {
			strength++;
		}
	}
	
	public void growExperience() {
		if(expirence!=5) {
			expirence++;
		}
		addHp(Settings.HP_POINTS_AFTER_FIGHT);
	}
	
	public int getHp() {
		return hp;
	}

	public void setHp(int hp) {
		this.hp += hp;
	}
	
	public void hurt(int hp) {
		this.hp -= hp;
		if(this.hp<=0) {
			die();
		}
	}
	
	public void addHp(int hp) {
		this.hp += hp;
	}
	
	public Genre getGenre() {
		return genre;
	}

	public int getExpirence() {
		return expirence;
	}

	public void setExpirence(int expirence) {
		this.expirence = expirence;
	}

	public Ant(Species species) {
		this.species = species;
		this.genre = Genre.randomGenre();
		this.expirence = 1;
		
		switch(species) {
			case BLACK: randomPosition(1); break;
			case YELLOW: randomPosition(2); break;
			case GREEN: randomPosition(3); break;
			case RED: randomPosition(4); break;
		}
		
	}
	
	
	public void randomPosition(int quarter)  {
		setMyOwnField(quarter);
		int randX = 0;
		int randY = 0;
		try {
			while(true) {
				Random random = new Random();			
				
				randX = random.nextInt(Settings.HEIGHT/2/Settings.ANT_SIZE) + getMinMyX();	
				randY = random.nextInt(Settings.WIDTH/2/Settings.ANT_SIZE) + getMinMyY();	
				if(Core.BATTLEFIELD[randX][randY].isBlank()) {
					this.x = randX;
					this.y = randY;
					break;
				} else {
					continue;
				}
			}
		} catch (NullPointerException e) {
			//System.out.println("Cwiartka nie zostaja wybrana");
		}
	}
	
	/**
	 * Ustawienie przedzialow
	 * @return
	 */
	public void setMyOwnField(int quarter) {
		int height = Settings.HEIGHT/Settings.ANT_SIZE;
		int width = Settings.WIDTH/Settings.ANT_SIZE;
		if (quarter == 1) {
			setMinMyX(width/2);
			setMaxMyX(width);
			setMinMyY(0);
			setMaxMyY(height/2);			
		} else if(quarter == 2) {
			setMinMyX(0);
			setMaxMyX(width/2);
			setMinMyY(0);
			setMaxMyY(height/2);			
		} else if(quarter == 3) {
			setMinMyX(0);
			setMaxMyX(width/2);
			setMinMyY(height/2);
			setMaxMyY(height);			
		} else if(quarter == 4) {
			setMinMyX(width/2);
			setMaxMyX(width);
			setMinMyY(height/2);
			setMaxMyY(height);			
		}
	
	}

	public int getMinMyX() {
		return minMyX;
	}

	public void setMinMyX(int minMyX) {
		this.minMyX = minMyX;
	}

	public int getMinMyY() {
		return minMyY;
	}

	public void setMinMyY(int minMyY) {
		this.minMyY = minMyY;
	}

	public int getMaxMyX() {
		return maxMyX;
	}

	public void setMaxMyX(int maxMyX) {
		this.maxMyX = maxMyX;
	}

	public int getMaxMyY() {
		return maxMyY;
	}

	public void setMaxMyY(int maxMyY) {
		this.maxMyY = maxMyY;
	}

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}
	
	public void die() {
		//System.out.println("marla");
		
		if(species == Species.BLACK) {
			synchronized(Core.antsBlack) {
				Core.antsBlack.remove(this);
			}
		} else if(species == Species.RED){
			synchronized(Core.antsRed) {	
				Core.antsRed.remove(this);
			}
		} else if(species == Species.YELLOW) {
			synchronized(Core.antsYellow) {
				Core.antsYellow.remove(this);
			}
		} else if(species == Species.GREEN) {
			synchronized(Core.antsGreen) {
				Core.antsGreen.remove(this);
			}
		}
		Core.BATTLEFIELD[getX()][getY()].setBlank(true);
		Core.BATTLEFIELD[getX()][getY()].setAnt(null);
	}
	
	public void move(int x, int y) {
		//System.out.println("Mrowka z gatunku " + getSpecies() + " poszla z =" + getX() + " y= " + getY());
		Core.BATTLEFIELD[getX()][getY()].setBlank(true);
		Core.BATTLEFIELD[getX()][getY()].setAnt(null);
		setX(x);
		setY(y);
		if(Core.BATTLEFIELD[getX()][getY()].isBlank()) {
			Core.BATTLEFIELD[getX()][getY()].setAnt(this);
			////System.out.println("Mrowka z gatunku " + getSpecies() + " poszla w x =" + getX() + " y= " + getY());
		} else {
			//System.out.println("BLAD! ruch w niedozwolone miejsce" );
		}
		
		
	}
	


}
